package com.itheima.controller.admin;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.config.SecurityConfiguration;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
@Slf4j
public class LoginController {
    //获得登录用户的用户名
    @GetMapping("/getLoginUsername")
    @ApiOperation("获得登录用户的用户名")
    public Result getLoginUsername() {
        //用户对象
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(user);
        if (user != null) {
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS ,user.getUsername());
        }
            return new Result(false , MessageConstant.GET_USERNAME_FAIL );

    }
}
